VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsDatabaseModules"
Attribute VB_GlobalNameSpace = True
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Public pckCode As String
Public pckDesc As String
Public pckOther As String

Public Sub DBConnect()
    Set cn = New ADODB.Connection
    cn.CursorLocation = adUseClient
    cn.ConnectionString = sConnString
    cn.Open
    If cn.State = 1 Then SetConnection = True
End Sub

Public Sub LoadPicker(sParam As String)
    On Error GoTo ErrHandler
    Dim tmpCN  As New Connection
    tmpCN.CursorLocation = adUseClient
    tmpCN.ConnectionString = sConnString_
    tmpCN.Open

    frmPicker.pckRS.Open "exec se_Picker '" & sParam & "'", tmpCN, adOpenStatic, adLockReadOnly
    frmPicker.Show vbModal
    
    pckCode = sPCKCode
    pckDesc = sPCKDesc
    pckOther = sPCKOthers
ErrHandler:
    If Err.Number <> 0 Then
        MsgBox Err.Description, vbCritical, ProjectVersion
    End If
End Sub


Public Function GenAutoNum(sModule As String, sType As String) As String
    Dim tmpCN  As New Connection
    tmpCN.CursorLocation = adUseClient
    tmpCN.ConnectionString = sConnString_
    tmpCN.Open
    
    Dim tmpRS As New Recordset
    tmpRS.Open "select dbo.[sef_GenNum]('" & sModule & "','" & sType & "') as cString", tmpCN, adOpenStatic, adLockReadOnly
    GenAutoNum = IIf(IsNull(tmpRS!cString), "", tmpRS!cString)
    
    tmpRS.Close
    tmpCN.Close
    Set tmpRS = Nothing
    Set tmpCN = Nothing
End Function

Public Sub FirstRecord(tmpRS As Recordset)
    tmpRS.MoveFirst
    Beep
End Sub

Public Sub PreviousRecord(tmpRS As Recordset)
    If tmpRS.Bookmark = 1 Then
        tmpRS.MoveFirst
        Beep
    Else
        tmpRS.MovePrevious
    End If
End Sub

Public Sub NextRecord(tmpRS As Recordset)
    If tmpRS.Bookmark = tmpRS.RecordCount Then
        tmpRS.MoveLast
        Beep
    Else
        tmpRS.MoveNext
    End If
End Sub

Public Sub LastRecord(tmpRS As Recordset)
    tmpRS.MoveLast
    Beep
End Sub

Public Sub PopulateCombo(sField As String, sTable As String, tmpCbo As Object, Optional sCond As String)
    Dim i As Integer
    
    Dim tmpCN  As New Connection
    tmpCN.CursorLocation = adUseClient
    tmpCN.ConnectionString = sConnString_
    tmpCN.Open
    
    Dim tmpRS As New Recordset
    tmpRS.Open "SELECT " & sField & " FROM " & sTable & IIf(sCond = "", "", " WHERE " & sCond), tmpCN, adOpenStatic, adLockReadOnly
    
    tmpCbo.Clear
    For i = 0 To tmpRS.RecordCount - 1
        tmpCbo.AddItem tmpRS.Fields(sField)
        tmpRS.MoveNext
    Next i
    tmpRS.MoveFirst
    tmpCbo.Text = tmpRS.Fields(sField)
    
    tmpRS.Close
    tmpCN.Close
    Set tmpRS = Nothing
    Set tmpCN = Nothing
End Sub

Public Function GetValFrTable(sField As String, sTable As String, sCond As String) As String
    On Error GoTo ErrHandler
    Dim tmpCN  As New Connection
    tmpCN.CursorLocation = adUseClient
    tmpCN.ConnectionString = sConnString_
    tmpCN.Open
    
    Dim tmpRS As New Recordset
    tmpRS.Open "SELECT " & sField & " FROM " & sTable & " WHERE " & sCond, tmpCN, adOpenStatic, adLockReadOnly
    
    If tmpRS.RecordCount <> 0 Then
        GetValFrTable = tmpRS.Fields(sField)
    End If
    
    tmpRS.Close
    tmpCN.Close
    Set tmpRS = Nothing
    Set tmpCN = Nothing
    
ErrHandler:
    If Err.Number <> 0 Then
        MsgBox Err.Description, vbCritical, ProjectVersion
        GetValFrTable = ""
    End If
End Function

Public Sub TranConnect(tmpCN As Connection)
    If tmpCN.State = 1 Then Exit Sub
    tmpCN.CursorLocation = adUseClient
    tmpCN.ConnectionString = sConnString_
    tmpCN.Open
End Sub

Public Sub TranRecordset(sTable As String, tmpRS As Recordset, Optional tmpCN As Connection = Nothing, Optional sCond As String = "")
    Dim lCN As Boolean
    If Not (tmpCN Is Nothing) Then lCN = True
    If tmpRS.State = 1 Then tmpRS.Close
    tmpRS.Source = "SELECT * FROM " & sTable & IIf(sCond = "", "", " WHERE " & sCond)
    tmpRS.CursorType = adOpenStatic
    tmpRS.LockType = adLockBatchOptimistic
    tmpRS.ActiveConnection = IIf(lCN, tmpCN, cn)
    tmpRS.Open
End Sub

Public Sub RSDisconnectCN(tmpRS As Recordset)
    tmpRS.ActiveConnection = Nothing
End Sub

Public Sub RSConnectCN(tmpRS As Recordset, tmpCN As Connection)
    tmpRS.ActiveConnection = tmpCN
End Sub


Public Sub DataSourceBind(tmpRS As Recordset, tmpForm As Object)
    Dim tmpObj As Object
    For Each tmpObj In tmpForm
        If (TypeName(tmpObj) = "oTextBox" And tmpObj.Tag <> ".") Then
            Set tmpObj.DataSource = tmpRS
        ElseIf TypeName(tmpObj) = "oCheckBox" Then
            Set tmpObj.DataSource = tmpRS
        ElseIf (TypeName(tmpObj) = "oDateBox") Then
            Set tmpObj.DataSource = tmpRS
        End If
    Next
End Sub

Public Sub DataFieldBind(tmpRS As Recordset, tmpForm As Object)
    Dim tmpObj As Object
    For Each tmpObj In tmpForm
        If (TypeName(tmpObj) = "oTextBox" And tmpObj.Tag <> ".") Then
            tmpObj.DataField = tmpRS.Fields(Mid$(tmpObj.Name, 4, Len(tmpObj.Name)))
        ElseIf TypeName(tmpObj) = "oCheckBox" Then
            tmpObj.DataField = tmpRS.Fields(Mid$(tmpObj.Name, 4, Len(tmpObj.Name)))
        ElseIf (TypeName(tmpObj) = "oDateBox") Then
            tmpObj.DataField = tmpRS.Fields(Mid$(tmpObj.Name, 4, Len(tmpObj.Name)))
        End If
    Next
End Sub

Public Sub DataUnbind(tmpForm As Object)
    Dim tmpObj As Object
    For Each tmpObj In tmpForm
        If (TypeName(tmpObj) = "oTextBox" And tmpObj.Tag <> ".") Then
            Set tmpObj.DataSource = Nothing
        ElseIf TypeName(tmpObj) = "oCheckBox" Then
            Set tmpObj.DataSource = Nothing
        ElseIf (TypeName(tmpObj) = "oDateBox") Then
            Set tmpObj.DataSource = Nothing
        End If
    Next
End Sub

Public Sub FilterRecord(sFieldName As String, sFieldValue As String, tmpRS As Recordset)
    On Error GoTo ErrHandler
    tmpRS.Filter = sFieldName & " = '" & sFieldValue & "'"
ErrHandler:
End Sub

Public Sub AuditLog(sModule As String, sTranNo As String, sEvent As String)
    Dim sSQL As String
    Dim tmpCN  As New Connection
    tmpCN.CursorLocation = adUseClient
    tmpCN.ConnectionString = sConnString_
    tmpCN.Open

    sSQL = "exec AuditTrail '" & sModule & "','" & sTranNo & "','" & sEvent & "','" & sUserID_ & "','" & sCompanyID_ & "'"
       
    tmpCN.Execute sSQL
    
    tmpCN.Close
    Set tmpCN = Nothing
End Sub
